package com.beiding.workordersystem.resource.entity;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.beiding.workordersystem.system.entity.UserEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class Resource {

    private String id;

    //资料名称
    private String name;

    //资料
    private String resource;

    //描述
    private String description;

    //主存id
    private String mainId;

    //入库人
    @TableField(strategy = FieldStrategy.IGNORED)
    private String saveUserId;

    //领用人
    @TableField(strategy = FieldStrategy.IGNORED)
    private String takeUserId;

    //出库人
    @TableField(strategy = FieldStrategy.IGNORED)
    private String removeUserId;

    @TableField(exist = false)
    private UserEntity saveUser;
    @TableField(exist = false)
    private UserEntity takeUser;
    @TableField(exist = false)
    private UserEntity removeUser;

    @TableField(strategy = FieldStrategy.IGNORED)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date saveTime;

    @TableField(strategy = FieldStrategy.IGNORED)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date takeTime;

    @TableField(strategy = FieldStrategy.IGNORED)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date removeTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date giveBackTime;

    //状态
    private Integer status;

    //异常状态
    @TableField(strategy = FieldStrategy.IGNORED)
    private Integer errorStatus;

    @TableField(exist = false)
    private String statusName;

    public void setStatus(Integer status) {
        this.status = status;
        if (statusName == null) {
            this.statusName = sn.get(status);
        }
    }

    //设置异常状态名称
    public void setErrorStatus(Integer errorStatus) {
        this.errorStatus = errorStatus;
        this.statusName = esn.get(errorStatus);
    }

    @TableField(exist = false)
    private Boolean hasChildren;

    @TableField(exist = false)
    private List<Resource> children;

    private static final Map<Integer, String> sn = new HashMap<>();

    private static final Map<Integer, String> esn = new HashMap<>();

    static {
        esn.put(-1, "入库申请退回");
        esn.put(-5, "出库申请退回");
        esn.put(-7, "归还申请退回");

        sn.put(1, "审核入库");
        sn.put(2, "可用");
        sn.put(3, "审核领用");
        sn.put(4, "使用中");
        sn.put(5, "审核出库");
        sn.put(6, "已出库");
        sn.put(7, "审核归还");
        sn.put(8, "部分资料使用中或审核领用");
    }

}
